import wx
from code.model_train import model,predict
import wx
import pickle

class ExamplePanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        f = open('vectorizer.txt', 'rb')
        self.vectorizer = pickle.load(f)
        f.close()
        f = open('classifier.txt', 'rb')
        self.classifier = pickle.load(f)
        f.close()

        # A button
        self.button_train = wx.Button(self, label="训练", pos=(120, 200))
        self.Bind(wx.EVT_BUTTON, self.OnClick_train, self.button_train)
        self.button_predict = wx.Button(self, label="预测", pos=(220, 200))
        self.Bind(wx.EVT_BUTTON, self.OnClick_predict, self.button_predict)

        # the edit control - one line version.
        self.lblname = wx.StaticText(self, label="新闻内容 :", pos=(20, 40))
        self.editname = wx.TextCtrl(self, value="输入新闻内容", pos=(100, 40), size=(220, -1), style=wx.TE_MULTILINE)

        self.resultname = wx.StaticText(self, label="结果 :", pos=(20, 160))
        self.result = wx.TextCtrl(self, value="点击预测得到结果", pos=(100, 160), size=(220, -1))

        self.tip = wx.StaticText(self, label="修改数据集后点击训练以保存模型", pos=(20, 25030))
    def OnClick_predict(self, event):
        result = predict(self.classifier, self.vectorizer, self.editname.GetValue())
        self.result.SetValue(result)

    def OnClick_train(self, event):
        model()

if __name__ == '__main__':
    app = wx.App(False)
    frame = wx.Frame(None,title='谣言检测工具',size=(360,300))
    panel = ExamplePanel(frame)
    frame.Show()
    app.MainLoop()